#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

#include<server/invokeproxy.h>
#include<server/export_symbol.h>
#include<cJSON/cJSON.h>
//#include<log4c/log.h>
#include "priv.h"
/**
 * 描述：RPC登录处理函数
 *
 * 参数：[cJSON]	[IN]	[arg]:RPC参数
 * 返回值：[错误]:NULL [正确][cJSON*]RPC返回值地址
 *
 * 作者：王学飞
 * 日期：2016-3-4
 *
 *
 * **/
cJSON *um_login(cJSON *args)
{
	printf("args = %s\n", cJSON_Print(args));
	cJSON *cj_ret;
	cJSON *cj_name,*cj_password;
	int ret;
	cj_ret = cJSON_CreateObject();
	if(!cj_ret){
		return cj_ret;
	}

	cj_name = cJSON_GetObjectItem(args, "account");
	if(!cj_name){
		cJSON_AddNumberToObject(cj_ret, "RetValue", LOGIN_ERROR);
		cJSON_AddStringToObject(cj_ret, "RetMesssage", "get account error!!!");
		return cj_ret;
	}

	if(strlen(cj_name->valuestring) >MAX_USERNAME_LEN){
		cJSON_AddNumberToObject(cj_ret, "RetValue", LOGIN_ERROR);
		cJSON_AddStringToObject(cj_ret, "RetMessage", "account too long!!!");
		return cj_ret;
	}
	
	cj_password = cJSON_GetObjectItem(args, "password");
	if(!cj_password){
		cJSON_AddNumberToObject(cj_ret, "RetValue", LOGIN_ERROR);
		cJSON_AddStringToObject(cj_ret, "RetMessage", "get password error!!!!!");
		return cj_ret;
	}

	if(strlen(cj_password->valuestring) >= MAX_PASSWORD_LEN){
		cJSON_AddNumberToObject(cj_ret, "RetValue", LOGIN_ERROR);
		cJSON_AddStringToObject(cj_ret, "RetMessage", "password too long !!!!");
	}


	ret = um_login_dal(cj_ret, cj_name->valuestring, cj_password->valuestring);
	if(ret){
		cJSON_AddNumberToObject(cj_ret, "RetValue",LOGIN_ERROR);
		cJSON_AddStringToObject(cj_ret, "RetMessage", "invoke dal error!!!!!");
	}
	//iv_inner_call("logout",NULL,cj_ret);
	inner_broad_cast(LOGIN,NULL);

	return cj_ret;
}
/**
 * 描述：RPC用户注销处理
 *
 * 参数：[cJSON]	[IN]	[arg]:RPC参数
 * 返回值：[错误]:NULL [正确][cJSON*]RPC返回值地址
 *
 * 作者：王学飞
 * 日期：2016-3-4
 *
 * **/
cJSON *um_logout(cJSON *args)
{
	//LOG_DEBUG("INVOKE LOGOUT!!");
	return NULL;
}

static int bc_test(cJSON *args)
{
	//LOG_ERROR("BROAD TEST!!!");
}

static method_st method[] = {
	{"login", 1, um_login, NULL},
	{"logout", 2, um_logout, NULL},
};

static bc_app_st bc_method[]={
	{LOGIN,bc_test,NULL},
	{LOGOUT,bc_test,NULL}
};

EXPORT_SYMBOL(method);
EXPORT_BROADCAST(bc_method);
